package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeBaseBatchDeleteDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeBaseDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeBaseDetailDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeBaseUpdateDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeCatalogueAddDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeCatalogueDelDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeCatalogueSortDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeCatalogueUpdateDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeHighlighterListDTO;
import com.xbongbong.pro.knowledge.base.pojo.dto.KnowledgeShowDTO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeBaseListVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeCatalogueListVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeCloudCallRedirectsVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeDetailsVO;
import com.xbongbong.pro.knowledge.base.pojo.vo.KnowledgeHighlighterListVO;
import com.xbongbong.saas.service.KnowledgeBaseService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @Author mingliang
 * @Description 知识库功能接口
 * @Date 2022/6/22 10:38
 **/
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/knowledge")
public class KnowledgeBaseController {

    @Resource
    private KnowledgeBaseService knowledgeBaseService;
    @Resource
    private PaasRedisHelper paasRedisHelper;


    /**
     * @Author mingliang
     * @Description 添加知识库
     * @Date 2022/6/22 15:36
     * @param knowledgeBaseDTO
     * @param br
     **/
    @RequestMapping(value = "/base/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addKnowledgeBase(@RequestBody @Valid KnowledgeBaseDTO knowledgeBaseDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(knowledgeBaseDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(knowledgeBaseDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = knowledgeBaseService.addKnowledgeBase(knowledgeBaseDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 修改知识库
     * @Date 2022/6/22 15:37
     * @param knowledgeBaseUpdateDTO
     * @param br
     **/
    @RequestMapping(value = "/base/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateKnowledgeBase(@RequestBody @Valid KnowledgeBaseUpdateDTO knowledgeBaseUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.updateKnowledgeBase(knowledgeBaseUpdateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 知识库列表查询
     * @Date 2022/6/22 19:04
     * @param baseDTO
     * @param br
     **/
    @RequestMapping(value = "/base/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeBaseList(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeBaseListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeBaseListVO knowledgeBaseListVO = knowledgeBaseService.knowledgeBaseList(baseDTO);
                response = new XbbResponse<>(knowledgeBaseListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/base/batchDelete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String baseBatchDelete(@RequestBody @Valid KnowledgeBaseBatchDeleteDTO knowledgeBaseBatchDeleteDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.baseBatchDelete(knowledgeBaseBatchDeleteDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 知识库删除接口
     * @Date 2022/6/24 15:21
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/base/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeBaseDelete(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.knowledgeBaseDelete(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 目录新增接口
     * @Date 2022/6/24 15:26
     * @param knowledgeCatalogueAddDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String addKnowledgeCatalogue(@RequestBody @Valid KnowledgeCatalogueAddDTO knowledgeCatalogueAddDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(knowledgeCatalogueAddDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(knowledgeCatalogueAddDTO)) {
                        response = new XbbResponse<>(new XbbException(SystemErrorCodeEnum.API_ERROR_100003));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = knowledgeBaseService.addKnowledgeCatalogue(knowledgeCatalogueAddDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 目录修改接口
     * @Date 2022/6/28 13:50
     * @param knowledgeCatalogueUpdateDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateKnowledgeCatalogue(@RequestBody @Valid KnowledgeCatalogueUpdateDTO knowledgeCatalogueUpdateDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.updateKnowledgeCatalogue(knowledgeCatalogueUpdateDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 目录删除接口
     * @Date 2022/6/28 15:02
     * @param knowledgeCatalogueDelDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/delete", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String deleteKnowledgeCatalogue(@RequestBody @Valid KnowledgeCatalogueDelDTO knowledgeCatalogueDelDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.deleteKnowledgeCatalogue(knowledgeCatalogueDelDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 目录查询
     * @Date 2022/6/27 15:39
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeCatalogueList(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeCatalogueListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeCatalogueListVO knowledgeCatalogueListVO = knowledgeBaseService.knowledgeCatalogueList(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(knowledgeCatalogueListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 目录拖动排序接口
     * @Date 2022/6/27 15:39
     * @param knowledgeCatalogueSortDTO
     * @param br
     **/
    @RequestMapping(value = "/catalogue/sort", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String knowledgeCatalogueSort(@RequestBody @Valid KnowledgeCatalogueSortDTO knowledgeCatalogueSortDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.knowledgeCatalogueSort(knowledgeCatalogueSortDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 高亮查询接口
     * @Date 2022/7/11 15:48
     * @param knowledgeHighlighterListDTO
     * @param br
     **/
    @RequestMapping(value = "/highlighter/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String highlighterList(@RequestBody @Valid KnowledgeHighlighterListDTO knowledgeHighlighterListDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeHighlighterListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeHighlighterListVO knowledgeHighlighterListVO = knowledgeBaseService.highlighterList(knowledgeHighlighterListDTO);
                response = new XbbResponse<>(knowledgeHighlighterListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 点击使用一次
     * @Date 2022/7/11 15:51
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/hot/autoIncrement", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String hotAutoIncrement(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                // 判断是否跳过重复提交校验
                if (!Boolean.TRUE.equals(knowledgeBaseDetailDTO.getIgnoreConCheck())) {
                    //重复提交校验
                    if (!paasRedisHelper.checkConcurrentLock(knowledgeBaseDetailDTO)) {
                        response = new XbbResponse<>(new XbbException(ErrorCodeEnum.API_SUCCESS));
                        return JSON.toJSONString(response);
                    }
                }
                BaseVO baseVO = knowledgeBaseService.hotAutoIncrement(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    /**
     * @Author mingliang
     * @Description 云呼展开详情接口
     * @Date 2022/7/19 10:41
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/cloudCall/getDetails", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cloudCallGetDetails(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeDetailsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeDetailsVO knowledgeContentVO = knowledgeBaseService.cloudCallGetDetails(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(knowledgeContentVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }



    /**
     * @Author mingliang
     * @Description 查询使用次数最多的15条热点数据
     * @Date 2022/7/11 16:00
     * @param knowledgeBaseDetailDTO
     * @param br
     **/
    @RequestMapping(value = "/hot/data", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String newKnowledgeData(@RequestBody @Valid KnowledgeBaseDetailDTO knowledgeBaseDetailDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeHighlighterListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeHighlighterListVO knowledgeHighlighterListVO = knowledgeBaseService.newKnowledgeData(knowledgeBaseDetailDTO);
                response = new XbbResponse<>(knowledgeHighlighterListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * @Author mingliang
     * @Description 云呼转跳参数接口
     * @Date 2022/7/19 16:04
     * @param baseDTO
     * @param br
     **/
    @RequestMapping(value = "/cloudCall/redirects", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String cloudCallRedirects(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) throws Exception {
        XbbResponse<KnowledgeCloudCallRedirectsVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                KnowledgeCloudCallRedirectsVO knowledgeBaseListVO = knowledgeBaseService.cloudCallRedirects(baseDTO);
                response = new XbbResponse<>(knowledgeBaseListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }


    /**
     * 首页知识库显示标识
     * @param knowledgeShowDTO
     * @param br
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/homePage/show", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String homePageShowKnowledge(@RequestBody @Valid KnowledgeShowDTO knowledgeShowDTO, BindingResult br) throws Exception {
        XbbResponse<BaseVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                BaseVO baseVO = knowledgeBaseService.homePageShowKnowledge(knowledgeShowDTO);
                response = new XbbResponse<>(baseVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

}
